package com.hz.sdka.sdka;

import android.content.Context;

import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.AdError;
import com.anythink.nativead.api.ATNative;
import com.anythink.nativead.api.ATNativeAdView;
import com.anythink.nativead.api.ATNativeNetworkListener;
import com.hz.sdka.commona.api.feed.OnHZFeedLoadListener;
import com.hz.sdka.commona.api.feed.UnifiedFeedApi;
import com.hz.sdka.commona.base.BaseAd;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.type.HZAdType;

import java.util.HashMap;
import java.util.Map;

public class FeedAda extends BaseAd implements UnifiedFeedApi {
    private OnHZFeedLoadListener onHZFeedLoadListener;
    private ATNative atNative;

    public FeedAda(Context context, String placeId) {
        super(placeId, HZAdType.FEED.indexOf());
        atNative = new ATNative(context, placeId, new ATNativeNetworkListener() {
            @Override
            public void onNativeAdLoaded() {
                ATNativeAdView anyThinkNativeAdView = new ATNativeAdView(context);
                AdNativea topOnNativeAd = new AdNativea(atNative, anyThinkNativeAdView, hzAdInfo);
                onHZFeedLoadListener.onFeedLoaded(topOnNativeAd);
            }

            @Override
            public void onNativeAdLoadFail(AdError adError) {
                onHZFeedLoadListener.onFeedLoadFailed(new HZAdError(adError.getCode(),
                        adError.getDesc(),
                        adError.getPlatformCode(),
                        adError.getPlatformMSG()));
            }
        });
    }

    @Override
    public void setListener(OnHZFeedLoadListener listener) {
        onHZFeedLoadListener = listener;
    }

    @Override
    public void setAdSize(int width, int height) {
        Map<String, Object> localMap = new HashMap<>();
        localMap.put(ATAdConst.KEY.AD_WIDTH, width);
        localMap.put(ATAdConst.KEY.AD_HEIGHT, height);
        atNative.setLocalExtra(localMap);
    }


    @Override
    public void load() {
        atNative.makeAdRequest();
    }

    /**
     * @hide 信息流不需要该方法
     */
    @Deprecated
    @Override
    public boolean isReady() {
        return false;
    }
}
